using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            //添加 3 个类，分别实现 IComparer<T> 接口，实现对 Student 类的三个字段的排序。
            //1、学生类：学号、姓名、年龄
            //2、请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息。
            //3、重复 的学号不能添加。
            //4、查询学生信息功能中有：1、查询所有（按学号排序）2、查询所有（按姓名排序），
            //3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出
            List<Students> list = new List<Students>();
            Hashtable ht = new Hashtable();

            Students s1 = new Students("招财", 18, 1003);
            Students s2 = new Students("进宝", 20, 1002);
            Students s3 = new Students("恭喜", 19, 1001);
            Students s4 = new Students("发财", 19, 1004);
            Students s5 = new Students("平安", 22, 1006);
            Students s6 = new Students("富贵", 23, 1005);

            list.Add(s1);
            list.Add(s2);
            list.Add(s3);
            list.Add(s4);
            list.Add(s5);
            list.Add(s6);

            ht.Add(s1.Stnum, s1);
            ht.Add(s2.Stnum, s2);
            ht.Add(s3.Stnum, s3);

            while (true)
            {
                Console.WriteLine("1、添加学生信息 2、查询学生信息 3、删除学生信息");
                int key = int.Parse(Console.ReadLine());
                switch (key)
                {
                    case 1:
                        //1、添加学生信息
                        Add(list, ht);
                        break;
                    case 2:
                        //2、查询学生信息
                        Refer(list, ht);
                        break;
                    case 3:
                        //3、删除学生信息
                        Delete(list, ht);
                        break;
                    default:
                        break;
                }
            }
        }
        public static void Add(List<Students> list, Hashtable ht)
        {
            Console.WriteLine("请输入添加的学号：");
            int stnum = int.Parse(Console.ReadLine());
            Boolean Flag = false;
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i].Stnum == stnum)
                {
                    Flag = true;
                }
            }
            if (Flag)
            {
                Console.WriteLine("已存在该学号！");
            }
            else
            {
                Console.WriteLine("请输入添加的姓名：");
                string name = Console.ReadLine();
                Console.WriteLine("请输入添加的年龄：");
                int age = int.Parse(Console.ReadLine());
                Students s5 = new Students(name, age, stnum);
                list.Add(s5);
                ht.Add(s5.Name, s5);
                Console.WriteLine("添加成功！");
            }
        }
        public static void Refer(List<Students> list, Hashtable ht)
        {
            bool Tout = false;
            while (true)
            {
                Console.WriteLine("1、查询所有（按年龄排序）2、查询所有（按学号排序），3、查询所有（按姓名排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
                int key1 = int.Parse(Console.ReadLine());
                switch (key1)
                {
                    case 1:
                        list.Sort(new AgeSort());
                        foreach (Students item in list)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    case 2:
                        list.Sort(new StunumSort());
                        foreach (Students item in list)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    case 3:
                        list.Sort(new NameSort());
                        foreach (Students item in list)
                        {
                            Console.WriteLine(item);
                        }
                        break;
                    case 4:
                        Console.WriteLine("请输入要查询的学号：");
                        int num1 = int.Parse(Console.ReadLine());
                        if (!ht.ContainsKey(num1))
                        {
                            Console.WriteLine("查无此人。");
                            return;
                        }
                        else
                        {
                            Console.WriteLine(list[num1]);
                        }
                        break;
                    case 5:
                        Tout = true;
                        break;
                }
            }
        }
        public static void Delete(List<Students> list, Hashtable ht)
        {
            Console.WriteLine("请输入你要删除学号：");
            int num2 = int.Parse(Console.ReadLine());
            if (ht.ContainsKey(num2))
            {
                ht.Remove(num2);
                Console.WriteLine("删除成功！");
            }
            else
            {
                Console.WriteLine("查无此人。");
            }
        }
    }
    internal class NameSort : IComparer<Students>
    {
        public int Compare(Students x, Students y)
        {
            return x.Name.CompareTo(y.Name);
        }
    }
    class AgeSort : IComparer<Students>
    {
        public int Compare(Students x, Students y)
        {
            return x.Age.CompareTo(y.Age);
        }
    }
    class StunumSort : IComparer<Students>
    {
        public int Compare(Students x, Students y)
        {
            return x.Stnum.CompareTo(y.Stnum);
        }
    }
    class Students
    {
        private string name;
        private int age;
        private int stnum;

        public string Name { get => name; set => name = value; }
        public int Age { get => age; set => age = value; }
        public int Stnum { get => stnum; set => stnum = value; }

        public Students(string name, int age, int stnum)
        {
            Name = name;
            Age = age;
            Stnum = stnum;
        }
        public override string ToString()
        {
            return $"姓名 {Name} 年龄 {Age} 学号 {Stnum}";
        }
    }
}